package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SCategoryGoodMapper;
import com.yuelife.service.pojo.SCategoryGood;
import com.yuelife.service.service.SCategoryGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SCategoryGood业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SCategoryGoodServiceImpl implements SCategoryGoodService {

    @Autowired
    private SCategoryGoodMapper sCategoryGoodMapper;


    /**
     * SCategoryGood条件+分页查询
     * @param sCategoryGood 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SCategoryGood> findPage(SCategoryGood sCategoryGood, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sCategoryGood);
        //执行搜索
        return new PageInfo<SCategoryGood>(sCategoryGoodMapper.selectByExample(example));
    }

    /**
     * SCategoryGood分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SCategoryGood> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SCategoryGood>(sCategoryGoodMapper.selectAll());
    }

    /**
     * SCategoryGood条件查询
     * @param sCategoryGood
     * @return
     */
    @Override
    public List<SCategoryGood> findList(SCategoryGood sCategoryGood){
        //构建查询条件
        Example example = createExample(sCategoryGood);
        //根据构建的条件查询数据
        return sCategoryGoodMapper.selectByExample(example);
    }


    /**
     * SCategoryGood构建查询对象
     * @param sCategoryGood
     * @return
     */
    public Example createExample(SCategoryGood sCategoryGood){
        Example example=new Example(SCategoryGood.class);
        Example.Criteria criteria = example.createCriteria();
        if(sCategoryGood!=null){
            // 商品分类id
            if(!StringUtils.isEmpty(sCategoryGood.getGoodCategoryPid())){
                    criteria.andEqualTo("goodCategoryPid",sCategoryGood.getGoodCategoryPid());
            }
            // 商品分类名称
            if(!StringUtils.isEmpty(sCategoryGood.getGoodCategoryPname())){
                    criteria.andLike("goodCategoryPname","%"+sCategoryGood.getGoodCategoryPname()+"%");
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sCategoryGoodMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SCategoryGood
     * @param sCategoryGood
     */
    @Override
    public void update(SCategoryGood sCategoryGood){
        sCategoryGoodMapper.updateByPrimaryKey(sCategoryGood);
    }

    /**
     * 增加SCategoryGood
     * @param sCategoryGood
     */
    @Override
    public void add(SCategoryGood sCategoryGood){
        sCategoryGoodMapper.insert(sCategoryGood);
    }

    /**
     * 根据ID查询SCategoryGood
     * @param id
     * @return
     */
    @Override
    public SCategoryGood findById(Integer id){
        return  sCategoryGoodMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SCategoryGood全部数据
     * @return
     */
    @Override
    public List<SCategoryGood> findAll() {
        return sCategoryGoodMapper.selectAll();
    }
}
